package com.feihao.zhou.workTest.demo03;

import java.util.*;

/**
 * 4. 分别用Comparable和Comparator两个接口对下列四位同学的成绩做降序排序，
 * 如果成绩一样，那在成绩排序的基础上按照年龄由小到大排序。
 * 姓名（String）	年龄（int）	分数（float）
 * liusan	              20	90.0F
 * lisi		      22	90.0F
 * wangwu		      20	99.0F
 * sunliu		      22	100.0F
 */
public class Test1 {
    public static void main(String[] args) {
        Student student1 = new Student("liusan", 20, 90.0F);
        Student student2 = new Student("lisi", 22, 90.0F);
        Student student3 = new Student("wangwu", 20, 99.0F);
        Student student4 = new Student("sunliu", 22, 100.0F);

        /*//Comparable接口做成绩降序排列；
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(student1);
        treeSet.add(student2);
        treeSet.add(student3);
        treeSet.add(student4);

        //迭代输出：
        Iterator<Student> iterator = treeSet.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            System.out.println(s);
        }*/

        //使用Comparator接口做：

        ArrayList<Student> list = new ArrayList<>();
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        System.out.println(list);

        System.out.println("==排序=====================排序====================排序==");
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = (int) (o2.getScore() * 10 - o1.getScore() * 10);
                return result == 0 ? o1.getAge() - o2.getAge() : result;
            }
        });

        Iterator<Student> iterator = list.iterator();
        while(iterator.hasNext()){
            Student s = iterator.next();
            System.out.println(s);
        }
    }
}
